<template>
  <div class="login-container">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
      auto-complete="on"
      label-position="left"
    >
      <div class="title-container">
        <h3 class="title">登录运营平台</h3>
      </div>
      <el-form-item prop="account">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="account"
          v-model.trim="loginForm.account"
          placeholder="请输入账号"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="off"
        />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model.trim="loginForm.password"
          :type="passwordType"
          placeholder="请输入密码"
          name="password"
          tabindex="2"
          auto-complete="off"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <el-form-item prop="checkCode" class="syl-item-left">
        <span class="svg-container">
          <svg-icon icon-class="code" />
        </span>
        <el-input
          :key="codeType"
          ref="checkCode"
          v-model.trim="loginForm.checkCode"
          placeholder="请输入验证码"
          tabindex="2"
          name="password"
          auto-complete="off"
        />
      </el-form-item>
      <div class="syl-item-right">
        <img
          alt=""
          :src="url"
          @click="getImg"
        >
      </div>
      <div class="syl-cache">
        <el-checkbox v-model="checked">记住密码</el-checkbox>
      </div>
      <el-button
        v-preventReClick
        :loading="loading"
        type="primary"
        style="width:100%;margin-bottom:30px;"
        @click.native.prevent="handleLogin"
      >登录
      </el-button>
    </el-form>
  </div>
</template>
<script>
import { getCheckCode } from '@/api/user'
import { setAdmin, setPassword, getAdmin, getPassword } from '@/utils/auth'

import { Encrypt, Decrypt } from '@/utils/AESUtil'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value.length <= 0) {
        callback(new Error('用户名不能为空'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length <= 0) {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    const validateCode = (rule, value, callback) => {
      if (value.length <= 0) {
        callback(new Error('验证码不能为空'))
      } else {
        callback()
      }
    }
    return {
      checked: false,
      loginForm: {
        account: '',
        password: '',
        checkCode: ''
      },
      loginRules: {
        account: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        checkCode: [{ required: true, trigger: 'blur', validator: validateCode }]
      },
      loading: false,
      passwordType: 'password',
      codeType: 'checkCode',
      redirect: undefined,
      // 查询参数
      queryNames: {
        account: 'gmt1'
      },
      url: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    // 在页面加载时从cookie获取登录信息
    const account = getAdmin('account')
    const password = getPassword('password')
    // 如果存在赋值给表单，并且将记住密码勾选
    if (account) {
      this.loginForm.account = account
      this.loginForm.password = Decrypt(password)
      this.checked = true
    }
    this.getImg()
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      const self = this
      const { account, password } = self.loginForm
      this.$refs['loginForm'].validate(valid => {
        if (valid) {
          if (self.checked) {
            setAdmin(account)
            setPassword(Encrypt(password))
          }
          self.loading = true
          self.$store.dispatch('user/login', this.loginForm).then(() => {
            self.$router.push({ path: '/' })
            self.loading = false
          }).catch(() => {
            self.loading = false
            self.getImg()
          })
        }
      })
    },
    /** 获取验证码 **/
    getImg() {
      getCheckCode().then(res => {
        const { data } = res
        this.url = data
      })
    }
  }
}
</script>

<style lang="scss">

/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0;
      -webkit-appearance: none;
      border-radius: 0;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    //border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }

  .syl-item-left {
    width: 50%;
    float: left;
  }

  .syl-item-right {
    width: 40%;
    float: right;

    img {
      width: 100%;
      height: 48px;
      cursor: pointer;
    }

    img[src=""] {
      opacity: 0;
    }
  }

}

.syl-cache {
  width: 100%;
  height: 45px;
  clear: both;
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  display: flex;
  justify-content: center;
  align-items: center;

  .login-form {
    width: 520px;/*px*/
    max-width: 100%;/*px*/
  }

  .tips {
    font-size: 14px;/*px*/
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0 auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
